﻿@using System.Diagnostics
@using System.Diagnostics.CodeAnalysis

@inherits InputBase<TraceLevel?>

<select class="form-select" @bind="_value">
    @foreach (var it in TraceLevels)
    {
        <option value="@it">@it</option>
    }
</select>

@code {

    private TraceLevel? _value
    {
        get
        {
            return Value;
        }
        set
        {
            if (Value != value)
            {
                Value = value;
                ValueChanged.InvokeAsync(value);
            }
        }
    }

    /// <summary>
    /// Available trace levels.
    /// </summary>
    [Parameter]
    public TraceLevel[] TraceLevels { get; set; } = new TraceLevel[]
      {
        TraceLevel.Off,
        TraceLevel.Info,
        TraceLevel.Verbose,
        TraceLevel.Warning,
        TraceLevel.Error,
      };

    protected override bool TryParseValueFromString(string? value,
     [MaybeNullWhen(false)] out TraceLevel? result,
     [NotNullWhen(false)] out string validationErrorMessage)
    {
        validationErrorMessage = "";
        try
        {
            if (value != null)
            {
                result = Enum.Parse<TraceLevel>(value);
            }
            else
            {
                result = TraceLevel.Off;
            }
        }
        catch (Exception ex)
        {
            result = TraceLevel.Off;
            validationErrorMessage = ex.Message;
            return false;
        }
        return true;
    }
}